/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bground.network.server;

import java.io.IOException;
import java.net.ServerSocket;
import java.util.ArrayList;

/**
 *
 * @author s6_kustan_e
 */
public class GameServer implements Runnable {

    ServerSocket s;
    ArrayList<Client> clients;
    public static final long min_update = 10;
    long elapsed_time;
    GameServer(int i) throws IOException {
        elapsed_time = 0;
        s = new ServerSocket(i);
        clients = new ArrayList<Client>();
        Thread t = new Thread(this);
    }


    public void run() {
        try {
            clients.add(new Client(s.accept()));

        } catch (IOException e) {
            System.out.println(e);
        }
    }

    void update(long l) {
        elapsed_time += l;
        if (elapsed_time < min_update) return;
        elapsed_time = 0;
        for(Client c : clients) {
            c.update(l);
        }
    }

    ArrayList<Command> getCommands() {
        ArrayList<Command> commands = new ArrayList<Command>();
        for(Client c : clients) {
            while (!c.inputBuffer.isEmpty())
            commands.add(c.inputBuffer.remove(0));
        }

        return commands;

    }
}
